package com.oncliedisk.administrator.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mogujie.tt.R;
import com.oncliedisk.administrator.DB.Dao;
import com.oncliedisk.administrator.adapter.FileAdapter;
import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.model.FileModel;


import java.util.List;

/**
 * Created by Administrator on 2015/12/4.
 */
public class SearchActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView img_back; //返回图片
    private ImageView img_search; //搜索图片
    private EditText content; //搜索关键字
    private ListView listview; //listVIew
    private FileAdapter mAdapter; //适配器
    private TextView reslut;
    private String currId = FileConfig.IS_ROOT; //当前ID
    private String currPosition = "0";
    private String paths = "网盘"; //路径
    private LinearLayout layout; //空的图片
    private  List<FileModel> fileByName;//当前搜索的集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initWeight();
        initListener();
    }

    private void initListener() {
        img_back.setOnClickListener(this);
        img_search.setOnClickListener(this);
        listview.setOnItemClickListener(this);
    }

    /**
     * 初始化控件
     */
    private void initWeight() {
        img_back = (ImageView) findViewById(R.id.search_back);
        img_search = (ImageView) findViewById(R.id.search_action);
        content = (EditText) findViewById(R.id.search_content);
        listview = (ListView) findViewById(R.id.search_listview);
        reslut = (TextView) findViewById(R.id.search_result);
        layout = (LinearLayout) findViewById(R.id.search_null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_back:
                //返回
                finish();
                break;
            case R.id.search_action:
                //搜索
                String name = content.getText().toString().trim();
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(SearchActivity.this,"关键字不可为空!",Toast.LENGTH_SHORT).show();
                }else{
                    //如果有输入关键字
                    searchByName(name);
                }
                break;
        }
    }

    /**
     * 根据关键字进行查询有关键字的数据
     * @param name
     */
    private void searchByName(String name) {
        fileByName = Dao.getInstance(SearchActivity.this).getFileByName(name);
        Toast.makeText(SearchActivity.this,"个数"+fileByName.size(),Toast.LENGTH_SHORT).show();
        mAdapter = new FileAdapter(fileByName,SearchActivity.this);
        listview.setAdapter(mAdapter);
        reslut.setText("搜索结果:"+fileByName.size()+"条");
        if(fileByName.size() <= 0){
            layout.setVisibility(View.VISIBLE);
        }else{
            layout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       //根据点击的条目，进行跳转页面显示
        FileModel item = (FileModel) mAdapter.getItem(position);
        int type = item.getType();
        if (type == FileConfig.TYPE_FOLDER) {
            //如果是文件夹,则打开文件夹
//            Intent intent = new Intent(SearchActivity.this,ShowFolder.class);
//            FileModel model = (FileModel) mAdapter.getItem(position);
//            intent.putExtra("data", model);
//            startActivity(intent);
            List<FileModel> data = Dao.getInstance(getActivity()).getFileById(item.getId());
            mAdapter = new FileAdapter(data, getActivity());
            listview.setAdapter(mAdapter);
            currId = currId + "/" + String.valueOf(item.getId());
            currPosition = currPosition + "/" + position;
            paths += "/" + item.getName();
            reslut.setText(paths);
        } else {
            Toast.makeText(getActivity(), "打开文件", Toast.LENGTH_SHORT).show();
        }
    }

    public Context getActivity(){
        return SearchActivity.this;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            if (currId.equals("0")) {
                //如果已经是目录了，就不退了
                finish();
                return true;
            }else{
                //如果不是目录
                paths = paths.substring(0, paths.lastIndexOf("/"));
                //把当前路径分割，获取当前目录的上一个目录的ID
                String[] split = currId.split("/");
                String[] positons = currPosition.split("/");
                String rsult = split[split.length - 2];
                //如果是0的话，就是返回到主页面了，就要获取所有数据，而不是根据ID获取指定数据
                if (rsult.equals("")) {
                    rsult = "0";
                }
                if (Integer.valueOf(rsult) == 0) {
                    mAdapter = new FileAdapter(fileByName, getActivity());
                } else {
                    mAdapter = new FileAdapter(Dao.getInstance(getActivity()).getFileById(Integer.valueOf(rsult)), getActivity());
                }
                listview.setAdapter(mAdapter);
                listview.setSelection(Integer.valueOf(positons[positons.length - 1]));
                reslut.setText(paths);
                //更新当前路径
                currId = currId.substring(0, currId.length() - 2);
                currPosition = currPosition.substring(0, currPosition.length() - 1);
                return false;
            }
        }
        return true;
    }
}
